package com.example.zzl.viewmodel.HomeFragment;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.R;
import com.example.zzl.adapter.HomeFragment.SpecialInfoAdapter;
import com.example.zzl.databinding.ActivitySpecialInfoBinding;
import com.example.zzl.model.HomeFragment.SpecialInfoModel;
import com.example.zzl.model.bean.SpecialInfo;
import com.example.zzl.utils.DisplayUtils;
import com.example.zzl.utils.specification.VolleySingleton;
import com.example.zzl.viewmodel.BaseViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ligg on 2017/2/11.
 */

public class SpecialInfoPresenter implements BaseViewModel{

    private final SpecialInfoModel model;
    public Context context;
    public SpecialInfoCallback callback;
    public ActivitySpecialInfoBinding db;
    private List<SpecialInfo.ListRecommendInfoBean> list;
    private SpecialInfoAdapter adapter;
    private com.example.zzl.databinding.HomeSpecialInfoHeadBinding headDb;

    public SpecialInfoPresenter(Context context, SpecialInfoCallback callback, ActivitySpecialInfoBinding db) {
        this.context = context;
        this.callback = callback;
        this.db = db;
        model = new SpecialInfoModel(context, this);
    }


    @Override
    public void init() {
        list = new ArrayList<>();
        adapter = new SpecialInfoAdapter(context, list, R.layout.home_special_info_item);
    }

    @Override
    public void initView() {
        db.setHeadTitle(callback.getHeadTitle());
        db.toolbar.setNavigationOnClickListener(callback);
        headDb = DataBindingUtil.bind(View.inflate(context, R.layout.home_special_info_head, null));
        headDb.getRoot().setMinimumHeight(DisplayUtils.displayMetrics.heightPixels / 2 - DisplayUtils.dipTopx(56));
        db.gv.addHeaderView(headDb.getRoot());
        db.gv.setAdapter(adapter);
    }

    @Override
    public void loadFinished() {
        SpecialInfo info = model.getInfo();
        loadHeadData(info);
        list.addAll(info.listRecommendInfo);
        adapter.notifyDataSetChanged();
    }

    private void loadHeadData(SpecialInfo info) {
        headDb.setSpecialInfo(info);
        final ImageView iv = headDb.iv;
        iv.setImageBitmap(null);
        iv.setTag(info.strSpecialImage);
        VolleySingleton.volleySingleton.getImageLoader().get(info.strSpecialImage, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (iv.getTag().equals(response.getRequestUrl())) {
                    iv.setImageBitmap(response.getBitmap());
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

    }

    @Override
    public void loadData(String parameter) {
        model.loadData(parameter);
    }


    public interface SpecialInfoCallback extends View.OnClickListener{
        String getHeadTitle();
    }
}
